package com.base.kongjian.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;
import cn.mysteel.util.StringUtils;

import com.base.kongjian.dao.KongjianDAO;
import com.base.kongjian.entity.Kongjian;
import com.base.kongjian.service.KongjianService;
import com.mysteel.page.Page;

@Service("kongjianService")
public class kongjianServiceImpl implements KongjianService
{
	@Autowired
	private KongjianDAO kongjianDao;

	@Override
	public void praise(long id, long praise_MId)
	{
		Kongjian kj = kongjianDao.get (id);
		
		if(ObjectUtils.notNull (kj))
		{
			String praiseId = kj.getPraise_MId ();
			
			if((StringUtils.notNull (praiseId)&&praiseId.indexOf (praise_MId+",")==-1)||(StringUtils.isNull (praiseId)))
			{
				praiseId = praiseId+praise_MId+",";
				kj.setPraise_MId (praiseId);
				
				kongjianDao.update (kj);
			}
		}
	}

	@Override
	public boolean hasPraise(long id, long praise_MId)
	{
		Kongjian kj = kongjianDao.get (id);
		
		if(ObjectUtils.notNull (kj))
		{
			String praiseId = kj.getPraise_MId ();
			
			if((StringUtils.notNull (praiseId)&&praiseId.indexOf (praise_MId+",")==-1)||(StringUtils.isNull (praiseId)))
			{
				
				return true;
			}
		}
		
		return false;
	}

	@Override
	public boolean canVisit(long id, long visitorId)
	{
		
		Kongjian kj = kongjianDao.get (id);
		
		if(ObjectUtils.notNull (kj))
		{
			String vId = kj.getVisitorId () ;
			
			if((StringUtils.notNull (vId)&&vId.indexOf (visitorId+",")==-1)||(StringUtils.isNull (vId)))
			{
				
				return true;
			}
		}
		
		return false;
	}

	@Override
	public List<Kongjian> kj_Level_Sort()
	{
		List<Long> list = kongjianDao.query (null);
		List<Kongjian> listKJ = null;
		if(ObjectUtils.notEmpty (list))
		{
			listKJ = new ArrayList<> ();
			
			for(long id : list)
			{
				listKJ.add (kongjianDao.get (id));
			}
		}

		return listKJ;
	}

	@Override
	public void saveKJ(Kongjian kongjian)
	{
		Assert.notNull (kongjian);
		
		long memberId =  kongjian.getMemberId ();
		
		Kongjian kj = kongjianDao.getByMemberId (memberId);
		
		if(ObjectUtils.isNull (kj))
		{
			kongjian.setCreateTime (System.currentTimeMillis ());
			
			kongjianDao.save (kongjian);
		}
	}

	@Override
	public Kongjian getKJById(long id)
	{
		
		return kongjianDao.get (id);
	}

	@Override
	public Page<Kongjian> queryKJ(Map<String, Object> params,int currentPageNum, int maxPageRowCount)
	{
		
		Page<Kongjian> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = kongjianDao.count(params);

		if (count > 0)
		{
			page = new Page<Kongjian>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> KongjianIds = kongjianDao.query(params);

			if (ObjectUtils.notEmpty(KongjianIds))
			{
				List<Kongjian> Kongjians = new ArrayList<Kongjian>(KongjianIds.size());

				for (long KongjianId : KongjianIds)
				{
					Kongjians.add(getKJById(KongjianId));
				}

				page.setDatas(Kongjians);
			}
		}

		return page;
	}

	@Override
	public void deleteKJById(long id)
	{
		Kongjian kj = kongjianDao.get (id);
		
		if(ObjectUtils.notNull (kj))
		{
			kongjianDao.delete (id);
		}
	}

	@Override
	public void updateKJ(Kongjian kongjian)
	{
		Assert.notNull (kongjian);

		Kongjian kj = kongjianDao.get (kongjian.getId ());
		
		if(ObjectUtils.notNull (kj))
		{
			kj.setLevel (kongjian.getLevel ());
			kj.setPraiseCount (kongjian.getPraiseCount ());
			kj.setVisitorId (kongjian.getVisitorId ());
			kj.setPraise_MId (kongjian.getPraise_MId ());
			kj.setOnlineTime (kongjian.getOnlineTime ());
			kj.setLoginTime (kongjian.getLoginTime ());
			kj.setLogoutTime (kongjian.getLogoutTime ());
			
			kongjianDao.update (kj);
		}
	}

	@Override
	public void login(long memberId)
	{
		Kongjian kj = kongjianDao.getByMemberId (memberId);
		
		if(ObjectUtils.notNull (kj))
		{
			kj.setLoginTime (System.currentTimeMillis ());
			kongjianDao.update (kj);
		}
	}

	@Override
	public String logOut(long memberId)
	{
		Kongjian kj = kongjianDao.getByMemberId (memberId);
		
		if(ObjectUtils.notNull (kj))
		{
			long currentTime = System.currentTimeMillis ();
			kj.setLogoutTime (currentTime);
			long loginTime = kj.getLoginTime ();
			long time = currentTime-loginTime;
			int level = kj.getLevel ();
			int targetLevel = level+1;
			long onLineTime = kj.getOnlineTime ();
			
			if(onLineTime+time>=targetLevel*3600000)
			{
				kj.setLevel (targetLevel);
				kj.setOnlineTime (0);
				kongjianDao.update (kj);
				
				return "shenji_success";
			}else
			{
				kj.setOnlineTime (onLineTime+time);
				
				kongjianDao.update (kj);
			}
			
		}
		
		return "shenji_failse";
	}

	@Override
	public Kongjian getKJByMemberId(long memberId)
	{
		
		return kongjianDao.getByMemberId (memberId);
	}

	@Override
	public long PV(long memberId)
	{
		Kongjian kj = kongjianDao.getByMemberId (memberId);
		 
		long num = 0;
		
		if(ObjectUtils.notNull (kj))
		{
			String visitor = kj.getVisitorId ();
			if(ObjectUtils.notNull (visitor))
			{
				String[] str = visitor.split (",");
				
				if(ObjectUtils.notEmpty (str))
				{
					num = str.length;
				}
			}
		}
		
		return num;
	}
}
